package com.gxuwz.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.MainUtil;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.Chart;
import com.gxuwz.pojo.DataEntity;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.SysUser;
import com.gxuwz.pojo.Topic;
import com.gxuwz.service.IDataService;
import com.gxuwz.service.IFormService;
import com.gxuwz.service.ITopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 数据获取处理
 * @author Administrator
 *
 */
public class ChartAction extends BaseAction implements Preparable, ModelDriven<Form>{

	protected static final String CHART_JSP = "/WEB-INF/page/chart/chart.jsp"; 
	
	

	@Autowired
	private IFormService formService;
	@Autowired
	private ITopicService topicService;
	@Autowired
	private IDataService dataService;
	
	private Form form;
	
	private Integer over;
	private Integer noOver;
	
	private String json;
	
	/**
	 * 跳转到图表统计
	 * @return
	 */
	public String toChart(){
		//查询完成情况
		DataEntity dataEntity = new DataEntity();
		form = formService.findById(form.getId());
		List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());
		List<DataEntity> dataList = dataService.find("data_"+form.getId(), dataEntity, topicList);
		over = 0;
		noOver = 0;
		for(DataEntity data:dataList){
			if(data.getDataState() == 0){
				noOver++;
			}else{
				over++;
			}
		}
		
		//查询选项分布

		
		setForwardView(CHART_JSP);
		return SUCCESS;
	}

	public void getChartData(){
		//查询完成情况
		DataEntity dataEntity = new DataEntity();
		form = formService.findById(form.getId());
		List<Topic> topicList = topicService.findByProperty("formId", form.getFormId());
		//大量文本无需统计,去除
		Iterator<Topic> it = topicList.iterator();
		while(it.hasNext()){
		    Topic x = it.next();
		    if(x.getTopicType() == 2 || x.getIsDelete() == 1){
		        it.remove();
		    }
		}
		List<DataEntity> dataList = dataService.find("data_"+form.getId(), dataEntity, topicList);
		//查询选项分布
		List<Chart> chartList = MainUtil.getChart(dataList, topicList);
		reJson(chartList);
		return;
	}
	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	@Override
	public Form getModel() {
		// TODO Auto-generated method stub
		return form;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}


	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		form = new Form();
	}

	public Integer getOver() {
		return over;
	}

	public void setOver(Integer over) {
		this.over = over;
	}

	public Integer getNoOver() {
		return noOver;
	}

	public void setNoOver(Integer noOver) {
		this.noOver = noOver;
	}


	
	
	
}
